package com.syh.jn.scm.service.impl.upstream.sale;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.support.entity.History;
import com.syh.jn.scm.domain.support.repository.HistoryRepository;
import com.syh.jn.scm.dto.support.HistoryDto;
import com.syh.jn.scm.dto.support.HistoryVo;
import com.syh.jn.scm.dto.upstream.sale.UpSaleOrderVo;
import com.syh.jn.scm.service.api.upstream.sale.UpSaleOrderHistoryService;
import com.syh.jn.scm.service.api.upstream.sale.UpSaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by soci.song on 2019/1/18.
 */
@Service
@Transactional
public class UpSaleOrderHistoryServiceImpl extends AbstractServiceImpl<HistoryDto,HistoryDto,HistoryVo,History,Long> implements UpSaleOrderHistoryService {

    @Resource(name = "upSaleHistoryRepositoryImpl")
    private HistoryRepository upSaleHistoryRepository;
    @Autowired
    private UpSaleOrderService upSaleOrderService;

    @Override
    protected AggregateRepository getAggregateRepository() {
        return this.upSaleHistoryRepository;
    }

    @Override
    public Long save(HistoryDto dto) {
        UpSaleOrderVo upSale = upSaleOrderService.getById(dto.getOriginalId());
        dto.buildHistory(JSONObject.parseObject(JSON.toJSONString(upSale)));
        return upSaleHistoryRepository.add(new History(dto));
    }
}
